package org.dromara.web.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.web.domain.bo.StudentBo;
import org.dromara.web.domain.vo.StudentVo;
import org.dromara.web.service.IStudentService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/student/student")
@SaIgnore
public class StudentController extends BaseController {

    private final IStudentService StudentService;

    /**
     * 查询学生信息列表
     */
    // @SaCheckPermission("Student:Student:list")
    @SaIgnore
    @GetMapping("/list")
    public TableDataInfo<StudentVo> list(StudentBo bo, PageQuery pageQuery) {
        return StudentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出学生信息列表
     */
    //  @SaCheckPermission("Student:Student:export")
    @SaIgnore
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StudentBo bo, HttpServletResponse response) {
        List<StudentVo> list = StudentService.queryList(bo);
        ExcelUtil.exportExcel(list, "学生信息", StudentVo.class, response);
    }

    /**
     * 获取学生信息详细信息
     *
     * @param id 主键
     */
    // @SaCheckPermission("Student:Student:query")
    @SaIgnore
    @GetMapping("/{id}")
    public R<StudentVo> getInfo(@NotNull(message = "主键不能为空")
                              @PathVariable Long id) {
        return R.ok(StudentService.queryById(id));
    }

    /**
     * 新增学生信息
     */
    //  @SaCheckPermission("Student:Student:add")
    @SaIgnore
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StudentBo bo) {
        return toAjax(StudentService.insertByBo(bo));
    }

    /**
     * 修改学生信息
     */
    //  @SaCheckPermission("Student:Student:edit")
    @SaIgnore
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StudentBo bo) {
        return toAjax(StudentService.updateByBo(bo));
    }

    /**
     * 删除学生信息
     *
     * @param ids 主键串
     */
    // @SaCheckPermission("Student:Student:remove")
    @SaIgnore
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(StudentService.deleteWithValidByIds(List.of(ids), true));
    }
}
